
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'getInfo':  return getInfo(event)

    default: return Promise.reject("unknown action")
  }
}

async function getInfo(event){
    var openid= event.openid
    return new Promise((reslove,reject)=>{
      return db.collection('user').where({
        _openid:openid
      })
      .field({
        _id:false,
        _openid:false,
         }).get()
         .then(res=>{
          reslove({code:200,message:"获取用户信息成功",data:res})
        })
        .catch(err=>{
          reject({code:300,message:"获得用户信息失败",data:err})
        })

    })
}
